package be.pluyckx.batteryprogress.application;

import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

import be.pluyckx.batteryprogress.R;
import be.pluyckx.batteryprogress.data.history.BatteryHistory;
import be.pluyckx.batteryprogress.database.BatteryLevelFileStorage;
import be.pluyckx.batteryprogress.logging.BatteryHistoryDump;
import be.pluyckx.batteryprogress.logging.LogcatDump;

/**
 * Created by Philip Luyckx on 8/08/13.
 */
public class BatteryProgressApplication extends Application {
  private static BatteryProgressApplication mApplication;

  private static final Handler mThreadHandler = new Handler();
  private static SharedPreferences mPreferences;

  public BatteryProgressApplication() {
    mApplication = this;
  }

  public static BatteryProgressApplication getApplication() {
    return mApplication;
  }

  public static File getDataDirectory() {
    return Constants.dataDirectory;
  }

  @Override
  public void onTerminate() {
    Constants.logcatDump.autoDumpFile(Constants.getDefaultLogcatDumpCallback());
    Constants.batteryHistoryDatabase.destroy();

    super.onTerminate();
  }

  static SharedPreferences getPreferences() {
    return mPreferences;
  }

  @Override
  public void onCreate() {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHander(Thread.getDefaultUncaughtExceptionHandler()));
    mPreferences = getSharedPreferences(Constants.PREFERENCES_NAME, MODE_PRIVATE);
    Constants.batteryHistoryDumpCallback = new BatteryHistoryDump.Callback() {
      @Override
      public void historyIsEmpty() {
        mThreadHandler.post(new Runnable() {
          @Override
          public void run() {
            Toast.makeText(BatteryProgressApplication.this,
                R.string.err_history_write_empty_list,
                Toast.LENGTH_LONG).show();
          }
        });
      }

      @Override
      public void done(final boolean writeSuccess) {
        mThreadHandler.post(new Runnable() {
          @Override
          public void run() {
            Toast.makeText(BatteryProgressApplication.this,
                (writeSuccess ? R.string.err_history_written : R.string.err_history_write_failed),
                Toast.LENGTH_LONG).show();
          }
        });
      }
    };

    Constants.logcatDumpCallback = new LogcatDump.Callback() {
      @Override
      public void done(final boolean writeSuccess) {
        mThreadHandler.post(new Runnable() {
          @Override
          public void run() {
            Toast.makeText(BatteryProgressApplication.this,
                (writeSuccess ? R.string.err_log_written : R.string.err_log_write_failed),
                Toast.LENGTH_LONG).show();
          }
        });
      }
    };

    Constants.batteryHistoryDatabase = new BatteryLevelFileStorage(getApplicationContext(), BatteryHistory.getHistory());
    try {
      Constants.batteryHistoryDatabase.read();
      Toast.makeText(this, R.string.err_history_loaded, Toast.LENGTH_LONG).show();
    } catch (IOException ex) {
      ex.printStackTrace();
      Toast.makeText(this, R.string.err_could_not_load_history, Toast.LENGTH_LONG).show();
    }

    super.onCreate();
  }

  private class UncaughtExceptionHander implements Thread.UncaughtExceptionHandler {
    private final Thread.UncaughtExceptionHandler mDefault;

    public UncaughtExceptionHander(Thread.UncaughtExceptionHandler defaultHandler) {
      mDefault = defaultHandler;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
      throwable.printStackTrace();

      Constants.logcatDump.autoDumpFile(Constants.getDefaultLogcatDumpCallback());

      mDefault.uncaughtException(thread, throwable);
    }
  }
}
